#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; ++i)
using namespace std;
char g[2][4];
unordered_map<string, pair<char, string>> pre;
unordered_map<string, int> dist;
void Set(string state) {
  rep(i, 0, 4) g[0][i] = state[i];
  for (int i = 7, j = 0; j < 4; --i, ++j) g[1][j] = state[i];
}
string get() {
  string res;
  for (int i = 0; i < 4; ++i) res += g[0][i];
  for (int i = 3; i >= 0; --i) res += g[1][i];
  return res;
}

string moveA(string state) {
  Set(state);
  rep(i, 0, 4) swap(g[0][i], g[1][i]);
  return get();
}

string moveB(string state) {
  Set(state);
  int v0 = g[0][3], v1 = g[1][3];
  for (int i = 3; i >= 0; --i) {
    g[0][i] = g[0][i - 1];
    g[1][i] = g[1][i - 1];
  }
  g[0][0] = v0, g[1][0] = v1;
  return get();
}

string moveC(string state) {
  Set(state);
  int v = g[0][1];
  g[0][1] = g[1][1];
  g[1][1] = g[1][2];
  g[1][2] = g[0][2];
  g[0][2] = v;
  return get();
}

string (*func[])(string) = {moveA, moveB, moveC};

int bfs(string start, string end) {
  if (start == end) return 0;
  queue<string> q;
  q.push(start);
  dist[start] = 0;
  while (q.size()) {
    auto t = q.front();
    q.pop();
    rep(i, 0, 3) {
      auto m = func[i](t);
      dist[m] = dist[t] + 1;
      pre[m] = {'A' + i, t};
      q.push(m);
      if (m == end) return dist[end];
    }
  }
  return -1;
}

int main() {
  int x;
  string start, end;
  rep(i, 0, 8) {
    cin >> x;
    end += char(x + '0');
  }
  rep(i, 1, 9) start += char('0' + i);
  int step = bfs(start, end);

  string res;
  while (end != start) {
    res += pre[end].first;
    end = pre[end].second;
  }
  reverse(res.begin(), res.end());

  cout << step << endl;
  if (step > 0) cout << res << endl;
}
